package com.lcz.wanandroid.utils;

import com.lcz.wanandroid.base.BaseApp;
import com.lcz.wanandroid.base.Person;
import com.lcz.wanandroid.base.SearchBean;
import com.lcz.wanandroid.dao.DaoMaster;
import com.lcz.wanandroid.dao.DaoSession;
import com.lcz.wanandroid.dao.PersonDao;
import com.lcz.wanandroid.dao.SearchBeanDao;

import java.util.List;

/**
 * Created by 李承泽 on 2019/5/9.
 */
public class SeracUtils {
    private static  volatile SeracUtils mSeracUtils;
    private final SearchBeanDao mSearchBeanDao;

    private SeracUtils (){
        DaoMaster.DevOpenHelper searc = new DaoMaster.DevOpenHelper(BaseApp.getInstance(), "searc");
        DaoMaster daoMaster = new DaoMaster(searc.getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        mSearchBeanDao = daoSession.getSearchBeanDao();
    }

    public static SeracUtils getmSeracUtils() {
        if(mSeracUtils==null){
            synchronized (SeracUtils.class){
                if(mSeracUtils==null){
                    mSeracUtils=new SeracUtils();
                }
            }
        }
        return mSeracUtils;
    }
    public void insert(SearchBean searchBean) {
        if (!has(searchBean)) {
            mSearchBeanDao.insertOrReplace(searchBean);
        }
    }
    public void DeleteAll(){
       mSearchBeanDao.deleteAll();
    }


    public List<SearchBean> QueryAll() {
        return mSearchBeanDao.queryBuilder().list();
    }

    public boolean has(SearchBean searchBean) {
        List<SearchBean> list = mSearchBeanDao.queryBuilder().where(SearchBeanDao.Properties.Title.eq(searchBean.getTitle())).list();
        if (list.size() > 0 && list != null) {
            return true;
        } else {
            return false;
        }
    }
}
